import {
  ArticleQueryActionType,
  getArticleDetailByIdApi,
  getArticleListApi,
} from '@/api/article';

import { AppDispatch } from '..';

export const getArticleListAction = ({
  channel_id,
  timestamp = Date.now(),
}: ArticleQueryActionType) => {
  return async (dispatch: AppDispatch) => {
    const res = await getArticleListApi({
      channel_id,
      timestamp,
    });
    // 请求的数据没有id,要覆盖回去
    dispatch({
      type: 'article/saveList',
      payload: { ...res.data, channel_id },
    });
    // console.log(res);
  };
};
export const updateArticleListByIdAction = ({
  channel_id,
  timestamp = Date.now(),
}: ArticleQueryActionType) => {
  return async (dispatch: AppDispatch) => {
    const res = await getArticleListApi({
      channel_id,
      timestamp,
    });
    // 请求的数据没有id,要覆盖回去
    dispatch({
      type: 'article/udpateListById',
      payload: { ...res.data, channel_id },
    });
    // console.log(res);
  };
};
// 获取文章详情
export const getArticleDetailByIdAction = (channel_id: string) => {
  return async (dispatch: AppDispatch) => {
    const res = await getArticleDetailByIdApi(channel_id);
    // 请求的数据没有id,要覆盖回去
    dispatch({
      type: 'article/saveDetail',
      payload: res.data,
    });
    // console.log(res);
  };
};
// 卸载组件清空内容
export const clearArticleDetailByIdAction = () => {
  return {
    type: 'article/clearDetail',
    payload: {},
  };
};
